yjx.controller('agencyController', ['$rootScope', '$scope', '$fetch', 'modalService', '$timeout', '$compile', 'ipCookie', '$state', 'detailService', function ($rootScope, $scope, $fetch, modalService, $timeout, $compile, ipCookie, $state, detailService) {
    $scope.code = '';
    $scope.telephone = ''
    $scope.showRelogin = false;
    $scope.okMsgTitle = "恭喜您已完成申请";
    $scope.okMsg = "您可以直接以健康管家的身份登录";
    $scope.agree = true;
    var registerApi = '/user/register/customer';
    var params = {};
    $scope.telephone = $rootScope.user.telephone || '';
    // var sucMsg = "恭喜您已完成申请！<br>您可以直接以健康管家的身份登录"
    /*解决android中软键盘弹出 将页面压缩的问题*/
    var HEIGHT = $('body').height();
    $('.m-agency').height(HEIGHT)
    $scope.apply = function () {
       
        if (!$scope.telephone || !$scope.telephone.match("^[1][34578][0-9]{9}$")) {
            modalService.mainMsgError("请输入正确格式的手机号");
            return;
        }
        if (!$scope.code2 || isNaN($scope.code2)) {
            modalService.mainMsgError("请输入数字验证码")
            return;
        }
        params.telephone = $scope.telephone;
        params.smscode = $scope.code2;
        if ($rootScope.identityTo == 1 || $rootScope.inviterUserId > 0) {
            registerApi = '/user/register/manager';
            if ($rootScope.inviterUserId) {
                params.inviterUserId = $rootScope.inviterUserId;
            } else {
                if (!$scope.code) {
                    modalService.mainMsgError("请输入邀请码")
                    return;
                }
                params.inviterCode = $scope.code;
            }
        } else {
            // alert($scope.username);
            if (!$scope.username) {
                modalService.mainMsgError("请输入您的姓名");
            }
            params.realname = $scope.username
        }
        register();
    }
    function register() {
        $fetch.fetch(
            registerApi,
            {
                params: params,
                type: 'post',
            }
        ).then(function (data) {
            $rootScope.changeState = null;
            $rootScope.changeStateName = null;
            if (data) {
                var userData = ipCookie('user');
                if (userData) {
                    userData.telephone = data.telephone;
                    userData.realname = data.realname;
                    ipCookie('user', userData, ipCookiesParam);
                }
                if (data.telephone) {
                    $rootScope.user.telephone = data.telephone;
                }
                if (data.realname) {
                    $rootScope.user.realname = data.realname;
                }
            }
            if ($rootScope.teamNo) {
                joinTeam();
            } else {
                detailService.killDetailAll();
                $state.reload();
            }
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    function joinTeam() {
        $fetch.fetch(
            '/group/join',
            {
                params: {
                    no: $rootScope.teamNo
                },
                type: 'post'
            }
        ).then(function (data) {
            joinSuc();
        }).catch(function (e) {
            if (e.code == 2009) {
                joinSuc();
            } else {
                modalService.mainMsgError(e.msg)
            }
        })
    }
    function joinSuc() {
        $rootScope.teamNo = null;
        $rootScope.inviteTeamId = null;
        $rootScope.changeState = null;
        $rootScope.changeStateName = null;
        $timeout(function(){
            modalService.mainMsgOk("您已成功加入该团队")
            detailService.killDetailAll();
            $state.go('index',{reload:true});
        })
        // $state.go('team',{teamNo:null},{reload:true});
    }
   
    $rootScope.changeState = true;
    $rootScope.changeStateName = 'close';
}])